/*
 * @(#)XfsimPlotDialog.java        2003/01/01
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */


package xfuzzy.xfsim.view;

import xfuzzy.lang.*;
import xfuzzy.util.*;
import xfuzzy.xfsim.model.*;
import xfuzzy.xfsim.view.listener.XfsimPlotActionListener;
import javax.swing.*;
import java.awt.*;

/**
 * Di�logo para configurar una representaci�n gr�fica
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfsimPlotDialog extends JDialog {

	//----------------------------------------------------------------------------//
	//                            COSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603062L;
	
	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Ventana principal de la aplicaci�n
	 */
	private Xfsim xfsim;
	
	/**
	 * Clase que contiene la configuraci�n de la representaci�n
	 */
	private XfsimPlot plot;
	
	/**
	 * Selector de la variable X
	 */
	private XComboBox xcombo;
	
	/**
	 * Selector de la variable Y
	 */
	private XComboBox ycombo;
	
	/**
	 * Grupo de botones para que s�lo uno pueda estar seleccionado
	 */
	private ButtonGroup group;
	
	/**
	 * Selector del tipo de representaci�n
	 */
	private JRadioButton[] radio;
	
	/**
	 * N�mero de variables de salida del sistema difuso
	 */
	private int outputlength;
	
	/**
	 * Marcador del resultado del di�logo
	 */
	private boolean result = false;
	
	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//
	
	/**
	 * Constructor
	 */
	public XfsimPlotDialog(Xfsim xfsim, XfsimPlot plot) {
		super(xfsim,"Xfsim",true);
		this.xfsim = xfsim;
		this.plot = plot;
		build();
	}
	
	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//
	
	/**
	 * Informa si se ha seleccionado la gr�fica o se ha cancelado
	 */
	public boolean getResult() {
		return result;
	}
		
	/**
	 * Selecciona las variables a representar y el tipo de gr�fica
	 */
	public void set() {
		for(int i=0; i<radio.length; i++) {
			if(radio[i].isSelected()) plot.setKind(i);
		}
		
		int xIndex = xcombo.getSelectedIndex();
		if(xIndex == 0) plot.setXvar("n");
		else if(xIndex == 1) plot.setXvar("t");
		else if(xIndex < outputlength+2) plot.setXvar("o"+(xIndex-2));
		else plot.setXvar("i"+(xIndex-outputlength-2));
		
		int yIndex = ycombo.getSelectedIndex();
		if(yIndex < outputlength) plot.setYvar("o"+yIndex);
		else plot.setYvar("i"+(yIndex-outputlength));

		result = true;
		setVisible(false);
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Construye el contenido de la ventana
	 */
	private void build() {
		Variable input[] = xfsim.getSpecification().getSystemModule().getInputs();
		Variable output[] = xfsim.getSpecification().getSystemModule().getOutputs();
		this.outputlength = output.length;
		
		String xlist[] = new String[2+input.length+output.length];
		xlist[0] = "_n";
		xlist[1] = "_t";
		for(int i=0; i<output.length; i++) xlist[i+2] = output[i].getName();
		for(int i=0; i<input.length;i++) xlist[i+2+output.length]=input[i].getName();
		String ylist[] = new String[input.length+output.length];
		for(int i=0; i<output.length; i++) ylist[i] = output[i].getName();
		for(int i=0; i<input.length;i++) ylist[i+output.length]=input[i].getName();
		
		xcombo = new XComboBox("X axis");
		xcombo.setList(xlist);
		String xvar = plot.getXvar();
		if(xvar.equals("n")) xcombo.setSelectedIndex(0);
		else if(xvar.equals("t")) xcombo.setSelectedIndex(1);
		else if(xvar.startsWith("o")) xcombo.setSelectedIndex(2+Integer.parseInt(xvar.substring(1)));
		else if(xvar.startsWith("i")) xcombo.setSelectedIndex(2+outputlength+Integer.parseInt(xvar.substring(1)));
			
		ycombo = new XComboBox("Y axis");
		ycombo.setList(ylist);
		String yvar = plot.getYvar();
		if(yvar.startsWith("o")) ycombo.setSelectedIndex(Integer.parseInt(yvar.substring(1)));
		else if(yvar.startsWith("i")) ycombo.setSelectedIndex(outputlength+Integer.parseInt(yvar.substring(1)));
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout( 3,2 ));
		group = new ButtonGroup();
		String radiolabel[] =
		{ "Line","Dots","Squares","Small circles","Medium circles","Large circles"};
		radio = new JRadioButton[radiolabel.length];
		for(int i=0; i<radio.length; i++) {
			radio[i] = new JRadioButton(radiolabel[i]);
			group.add(radio[i]);
			panel.add(radio[i]);
		}
		radio[plot.getKind()].setSelected(true);
		
		XfsimPlotActionListener listener = new XfsimPlotActionListener(this);
		String lb[] = { "Set", "Cancel" };
		XCommandForm form = new XCommandForm(lb,lb,listener);
		form.setCommandWidth(100);
		
		Container content = getContentPane();
		content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
		content.add(new XLabel("Graphical representation"));
		content.add(xcombo);
		content.add(ycombo);
		content.add(new XLabel("Graphical style"));
		content.add(panel);
		content.add(form);
		
		Point loc = xfsim.getLocationOnScreen();
		loc.x += 40; loc.y += 200;
		this.setLocation(loc);
		pack();
	}
}

